package com.ruoyi.grade.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.grade.mapper.ScoreMapper;
import com.ruoyi.grade.domain.Score;
import com.ruoyi.grade.service.IScoreService;

/**
 * 课程成绩Service业务层处理
 * 
 * @author ruoyi

 */
@Service
public class ScoreServiceImpl implements IScoreService 
{
    @Autowired
    private ScoreMapper scoreMapper;

    /**
     * 查询课程成绩
     * 
     * @param scoreId 课程成绩主键
     * @return 课程成绩
     */
    @Override
    public Score selectScoreByScoreId(Long scoreId)
    {
        return scoreMapper.selectScoreByScoreId(scoreId);
    }

    /**
     * 查询课程成绩列表
     * 
     * @param score 课程成绩
     * @return 课程成绩
     */
    @Override
    public List<Score> selectScoreList(Score score)
    {
        return scoreMapper.selectScoreList(score);
    }

    /**
     * 新增课程成绩
     * 
     * @param score 课程成绩
     * @return 结果
     */
    @Override
    public int insertScore(Score score)
    {

        // 设置创建时间
        score.setCreateTime(DateUtils.getNowDate());

        // 查询 user_id
        Long userId = scoreMapper.selectUserIdByUserName(score.getUserName());
        if (userId == null) {
            throw new RuntimeException("用户不存在：" + score.getUserName());
        }
        score.setUserId(userId);

        // 查询 course_id
        Long courseId = scoreMapper.selectCourseIdByCourseName(score.getCourseName());
        if (courseId == null) {
            throw new RuntimeException("课程不存在：" + score.getCourseName());
        }
        score.setCourseId(courseId);

        // 插入成绩记录
        return scoreMapper.insertScore(score);



    }

    /**
     * 修改课程成绩
     * 
     * @param score 课程成绩
     * @return 结果
     */
    @Override
    public int updateScore(Score score)
    {
        score.setUpdateTime(DateUtils.getNowDate());
        return scoreMapper.updateScore(score);
    }

    /**
     * 批量删除课程成绩
     * 
     * @param scoreIds 需要删除的课程成绩主键
     * @return 结果
     */
    @Override
    public int deleteScoreByScoreIds(Long[] scoreIds)
    {
        return scoreMapper.deleteScoreByScoreIds(scoreIds);
    }

    /**
     * 删除课程成绩信息
     * 
     * @param scoreId 课程成绩主键
     * @return 结果
     */
    @Override
    public int deleteScoreByScoreId(Long scoreId)
    {
        return scoreMapper.deleteScoreByScoreId(scoreId);
    }
}
